package _linklist;

import org.junit.Assert;
import org.junit.Test;

/**
 * 203. 移除链表元素
 * https://leetcode.cn/problems/remove-linked-list-elements/description/
 * https://programmercarl.com/0203.%E7%A7%BB%E9%99%A4%E9%93%BE%E8%A1%A8%E5%85%83%E7%B4%A0.html
 *
 * 给你一个链表的头节点 head 和一个整数 val ，请你删除链表中所有满足 Node.val == val 的节点，并返回 新的头节点 。
 *
 * 输入：head = [1,2,6,3,4,5,6], val = 6
 * 输出：[1,2,3,4,5]
 *
 * 输入：head = [], val = 1
 * 输出：[]
 *
 * 输入：head = [7,7,7,7], val = 7
 * 输出：[]
 */
public class leetcode_203_Java {

    @Test
    public void test_1() {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(4);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        ListNode r = removeElements(l1, 4);
        String actual = getActual(r);
        String expected = "1,2,3,";
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void test_2() {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(4);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        ListNode r = removeElements(l1, 1);
        String actual = getActual(r);
        String expected = "2,3,4,";
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void test_3() {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(4);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        ListNode r = removeElements(l1, 2);
        String actual = getActual(r);
        String expected = "1,3,4,";
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void test_4() {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(4);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        ListNode r = removeElements(l1, 5);
        String actual = getActual(r);
        String expected = "1,2,3,4,";
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void test_7() {
        ListNode l1 = new ListNode(7);
        ListNode l2 = new ListNode(7);
        ListNode l3 = new ListNode(7);
        ListNode l4 = new ListNode(7);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        ListNode r = removeElements(l1, 7);
        String actual = getActual(r);
        String expected = "";
        Assert.assertEquals(expected, actual);
    }

    private String getActual(ListNode r) {
        String actual = "";
        while (r != null) {
            actual = actual + r.val + ",";
            r = r.next;
        }
        return actual;
    }

    private ListNode removeElements(ListNode head, int val) {
        // T - N
        // S - 1
        if (null == head) {
            return null;
        }
        ListNode dummyHeader = new ListNode(-1, head);
        ListNode left = dummyHeader;
        ListNode right = null;

        while (true) {
            right = left.next;
            if (right == null) {
                break;
            }
            if (right.val != val) {
                left = right;
            } else {
                left.next = right.next;
            }
            System.out.println("loop");
        }
        return dummyHeader.next;
    }

    static private class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
